package com.xjscrm.console.web.chat;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.google.common.collect.Lists;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.common.util.LoginContext;
import com.xjscrm.console.dto.chat.*;
import com.xjscrm.console.service.customer.chat.ChatWordsLibraryGroupService;
import com.xjscrm.console.service.customer.chat.ChatWordsLibraryService;
import com.xjscrm.console.vo.LoginVO;
import com.xjscrm.console.vo.chat.ChatWordsLibraryGroupVO;
import com.xjscrm.console.vo.chat.ChatWordsLibraryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/15 11:54
 */
@Api(value = "话术管理", tags = "客服聊天|话术管理")
@RestController
@RequestMapping("/chat/words_library")
public class ChatWordsLibraryController {

    @Autowired
    private ChatWordsLibraryGroupService chatWordsLibraryGroupService;
    @Autowired
    private ChatWordsLibraryService chatWordsLibraryService;


    @PostMapping("/upload")
    @ApiOperation(value = "上传话术", notes = "上传话术")
    public Object upload(@RequestParam MultipartFile file, @RequestParam("type") Integer type) {
        LoginVO login = LoginContext.getLogin();
        try {
            EasyExcel.read(file.getInputStream(), ChatWordsLibraryUploadDTO.class, new ReadListener<ChatWordsLibraryUploadDTO>() {
                final List<ChatWordsLibraryUploadDTO> list = new ArrayList<>();

                @Override
                public void invoke(ChatWordsLibraryUploadDTO chatWordsLibraryUploadDTO, AnalysisContext analysisContext) {

                    list.add(chatWordsLibraryUploadDTO);
                    if (list.size() >= 100) {
                        chatWordsLibraryService.save(login.getMerId(), type, Lists.newArrayList(list));
                        list.clear();
                    }
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    chatWordsLibraryService.save(login.getMerId(), type, list);
                }
            }).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    @PostMapping("/get")
    @ApiOperation(value = "详情话术", notes = "详情话术", response = ChatWordsLibraryVO.class)
    public Object get(@RequestBody ChatWordsLibraryQueryDTO query) {
        if (Objects.isNull(query) || Objects.isNull(query.getId())) {
            throw new BusinessException("获取话术详情参数错误");
        }
        LoginVO login = LoginContext.getLogin();

        return chatWordsLibraryService.get(login.getMerId(), query.getId());
    }

    @PostMapping("/search")
    @ApiOperation(value = "查询话术", notes = "查询话术", response = ChatWordsLibraryVO.class)
    public Object search(@RequestBody ChatWordsLibraryQueryDTO query) {
        if (Objects.isNull(query)) {
            throw new BusinessException("查询参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        return chatWordsLibraryService.search(login, query);
    }

    @PostMapping("/sort")
    @ApiOperation(value = "排序话术", notes = "排序话术", response = ChatWordsLibraryVO.class)
    public Object sort(@RequestBody ChatWordsLibraryOrderDTO opt) {
        if (Objects.isNull(opt) || ObjectUtils.isEmpty(opt.getSortItems())) {
            throw new BusinessException("排序话术参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        return chatWordsLibraryService.sort(login, opt);
    }

    @PostMapping("/group/search")
    @ApiOperation(value = "查询话术分组", notes = "查询话术分组", response = ChatWordsLibraryGroupVO.class)
    public Object searchGroup(@RequestBody ChatWordsLibraryGroupQueryDTO query) {

        if (Objects.isNull(query) || Objects.isNull(query.getType())) {
            throw new BusinessException("查询参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        return chatWordsLibraryGroupService.search(login, query);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增话术", notes = "新增话术", response = Boolean.class)
    public Object add(@RequestBody ChatWordsLibraryOptDTO opt) {
        if (Objects.isNull(opt) || !ObjectUtils.allNotNull(opt.getContentType(), opt.getGroupId())
                || StringUtils.isAnyBlank(opt.getTitle(), opt.getContent())) {
            throw new BusinessException("创建话术参数失败");
        }
        LoginVO login = LoginContext.getLogin();
        chatWordsLibraryService.add(login, opt);
        return true;
    }

    @PostMapping("/group/add")
    @ApiOperation(value = "新增话术分组", notes = "新增话术分组", response = Boolean.class)
    public Object addGroup(@RequestBody ChatWordsLibraryGroupOptDTO opt) {

        if (Objects.isNull(opt) || Objects.isNull(opt.getType())
                || StringUtils.isBlank(opt.getName())) {
            throw new BusinessException("创建话术分组参数失败");
        }
        LoginVO login = LoginContext.getLogin();
        chatWordsLibraryGroupService.add(login, opt);
        return true;
    }

    @PostMapping("/group/sort")
    @ApiOperation(value = "排序话术分组", notes = "排序话术分组", response = Boolean.class)
    public Object sortGroup(@RequestBody ChatWordsLibraryGroupOrderDTO opt) {

        if (Objects.isNull(opt) || ObjectUtils.isEmpty(opt.getSortItems())) {
            throw new BusinessException("排序话术分组参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        chatWordsLibraryGroupService.sort(login, opt);
        return true;
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改话术", notes = "修改话术", response = Boolean.class)
    public Object edit(@RequestBody ChatWordsLibraryOptDTO opt) {

        if (Objects.isNull(opt) || !ObjectUtils.allNotNull(opt.getId())) {
            throw new BusinessException("修改话术参数失败");
        }
        LoginVO login = LoginContext.getLogin();
        chatWordsLibraryService.edit(login, opt);
        return true;
    }

    @PostMapping("/group/edit")
    @ApiOperation(value = "修改话术分组", notes = "修改话术分组", response = Boolean.class)
    public Object editGroup(@RequestBody ChatWordsLibraryGroupOptDTO opt) {

        if (Objects.isNull(opt) || Objects.isNull(opt.getId())) {
            throw new BusinessException("修改话术分组参数失败");
        }
        LoginVO login = LoginContext.getLogin();
        chatWordsLibraryGroupService.edit(login, opt);
        return true;
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除话术", notes = "删除话术", response = Boolean.class)
    public Object delete(@RequestBody ChatWordsLibraryOptDTO opt) {

        if (Objects.isNull(opt) || !ObjectUtils.allNotNull(opt.getId())) {
            throw new BusinessException("删除话术参数失败");
        }
        LoginVO login = LoginContext.getLogin();
        chatWordsLibraryService.delete(login, opt.getId());
        return true;
    }

    @PostMapping("/group/delete")
    @ApiOperation(value = "删除话术分组", notes = "删除话术分组", response = Boolean.class)
    public Object deleteGroup(@RequestBody ChatWordsLibraryGroupOptDTO opt) {

        if (Objects.isNull(opt) || Objects.isNull(opt.getId())) {
            throw new BusinessException("删除话术分组参数失败");
        }
        LoginVO login = LoginContext.getLogin();
        chatWordsLibraryGroupService.delete(login, opt.getId());
        return true;
    }
}
